package com.woniuxy.table.service.util;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;

/**
 * 二维码工具
 * @Author:debug (SteadyJack)
 * @Link: weixin-> debug0868  qq-> 1948831260
 * @Date: 2020/11/16 22:38
 **/
public class ZxingUtil {


    public String createImge(String text) throws Exception{
        int width=600;
        int height=600;
        String type = "png";
        String filekey =text+".png";
        HashMap map = new HashMap();
        map.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.H);
        map.put(EncodeHintType.CHARACTER_SET,"utf-8");
        map.put(EncodeHintType.MARGIN,1);

        BitMatrix encode = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height);

//        Path file = new File("E:\\父类路径\\iamgs\\"+storeId+"号商铺"+tableId+"号桌二维码.png").toPath();
//        MatrixToImageWriter.writeToPath(encode,type,file);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(encode,"PNG",byteArrayOutputStream);
        byte[] bytes = byteArrayOutputStream.toByteArray();
        AliyunOss oss = new AliyunOss();
        String url = oss.ossUploadByteStream(filekey, bytes);
        if (url == null) {
            return null;
        }
        return url;
    }

}
